package com.example.farmeri.ui.strategy;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.widget.NestedScrollView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.farmeri.R;
import com.example.farmeri.pojo.Role;
import com.example.farmeri.presenter.strategy.AddStrategyPresenter;
import com.example.farmeri.presenter.strategy.IAddStrategyContract;
import com.example.farmeri.presenter.strategy.IViewStrategyContract;
import com.example.farmeri.presenter.strategy.ViewStrategyPresenter;
import com.example.farmeri.tools.ResponseUtils;
import com.example.farmeri.ui.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 查看策略
 */
public class ViewStrategyActivity extends BaseActivity<ViewStrategyPresenter> implements
        View.OnClickListener , IViewStrategyContract {
    private PopupWindow pw;
    //编辑模式switch锁定
    @BindView(R.id.check_strategy_switch)
    Switch lock;
    //管理员
    @BindView(R.id.check_strategy_admin)
    Switch admin;
    //查看湿度
    @BindView(R.id.check_strategy_humidity)
    Switch humid;
    //查看光照
    @BindView(R.id.check_strategy_sunny)
    Switch sunny;
    //查看温度
    @BindView(R.id.check_strategy_temperature)
    Switch temp;
    //查看开发者账号
    @BindView(R.id.check_strategy_development)
    Switch develop;
    //查看农田主
    @BindView(R.id.check_strategy_farmer)
    Switch farm;
    //编辑和查看标题
    @BindView(R.id.ConstraintLayout_check_strategy)
    ConstraintLayout constraintStrategy;
    //底部弹框
    @BindView(R.id.check_strategy_linearlayout)
    LinearLayout linearstra;

    @BindView(R.id.checkStrategy_bottom_container)
    NestedScrollView strategy_bottom;

    @BindView(R.id.check_strategy_name)
    EditText name;
    @BindView(R.id.check_strategy_identy)
    EditText ident;
    @BindView(R.id.check_strategy_address)
    EditText address;
    @BindView(R.id.check_strategy_number)
    EditText number;
    @BindView(R.id.check_strategy_email)
    EditText email;
    @BindView(R.id.check_strategy_age)
    EditText age;
    @BindView(R.id.check_strategy_note)
    EditText note;
    @BindView(R.id.checkstrategy_relativelayout_title)
    RelativeLayout relativeLayout;
    @BindView(R.id.check_strategy_title)
    TextView title;
    @BindView(R.id.check_strategy_button)
    Button checkbutton;

    List<View> viewList=new ArrayList<>();
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_strategy);
        ButterKnife.bind(this);
        present = new ViewStrategyPresenter(this);
        present.getRole(getIntent().getStringExtra("id"));
        deviceLockCheckstrategy();
    }


    private void setData(Role role) {
        note.setText(role.getRemarks());
        number.setText(role.getTel());
        address.setText(role.getRequestPlace());
        name.setText(role.getName());
        age.setText(role.getAge());
        email.setText(role.getEmail());
        ident.setText(role.getIdentity());
    }


    //设置屏幕的编辑和锁定状态
    private void deviceLockCheckstrategy() {
        constraintStrategy.setOnClickListener(this);
        traversalView(constraintStrategy);

        //编辑滑动switch时页面的编辑和只读状态切换
        lock.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    title.setText("编辑策略");
                    setEditCannotedit(true);
                    //设置底部弹出框

                }else{
                    title.setText("查看策略");
                    setEditCannotedit(false);
                    //设置底部弹出框

                }
            }
        });

    }


    private void setEditCannotedit(boolean edit) {
        note.setEnabled(edit);
        number.setEnabled(edit);
        address.setEnabled(edit);
        name.setEnabled(edit);
        age.setEnabled(edit);
        email.setEnabled(edit);
        ident.setEnabled(edit);
        admin.setEnabled(edit);
        develop.setEnabled(edit);
        humid.setEnabled(edit);
        temp.setEnabled(edit);
        farm.setEnabled(edit);
        sunny.setEnabled(edit);
    }

    //获得ConstraintLayout里的所有控件
    private void traversalView(ConstraintLayout constraintLayout) {
        int i =constraintLayout.getChildCount();
        for(int j=0;j<i;j++){
            View view =constraintLayout.getChildAt(j);
            viewList.add(view);
        }
    }

    @Override
    public void onClick(View v) {

    }


    @Override
    public void onRoleDataResult(Role data) {
        setData(data);
    }

    @Override
    public void onRoleDataError(String msg) {
        showMessage(msg);
    }

    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }
}
